package com.mobileClientKernel.android.commonLayer;

import android.content.Context;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class PreferenceUtil {

    public static final String GESTURE_SETTING = "GESTURESETTING";
    public static final String SESSIONTIMER = "session_timer";
    public static final String CERTIFICATEVERISON = "CERTIFICATE_VERISON";
    public static final String CERTIFICATE = "CERTIFICATE";
    public static final String BJTIME = "bjtime";
    public static final String GESTUREINFO = "GestureInfo";
    public static final String SALT = "SALT";
    public static final String RANDKEY = "RANDKEY";
    public static final String HEADIMAGE = "headImg";
    public static final String URL = "url";
    public static final String ADURL = "adurl";
    public static final String ADTITLE = "adTitle";
    public static final String FLAG = "flag300";
    public static final String USERINFO = "USER_INFO";
    public static final String REGID = "RegId";
    public static final String MOBILE = "Mobile";
    public static final String GESTURESESSIONKEY = "GestureSessionKey";
    public static final String ISLOGIN = "isLogin";
    public static final String ISMIUI = "isMIUI";
    public static final String BANKSTATE = "bankstate";
    public static final String TIME = "time";
    public static final String SLOGEN = "slogen";
    public static final String CONTACTSPROMISSION = "contactspromission";
    public static final String CONTACTS = "contacts";
    public static final String SHOWHOMEADV = "showHomeAdv";
    public static final String SHOWBILLNOTICE = "showBillNotice";
    public static final String DEBUGKEY = "debug";
    public static final String SPLASHADKEY = "splashadkey";
    public static final String APPLOGKEY = "applogkey";

    public static final String MOBILESIGN = "mobileSign";
    public static final String BANKBEAN = "bankbean";


    public static final String HOTFIXVERSION = "hotfixversion";
    public static final String HOTFIXVERSIONNEW = "hotfixversionnew";
    public static final String HOTFIXVERSIONID = "hotfixversionid";
    public static final String HOTFIXMD5 = "hotfixmd5";
    public static final String HOTFIXPATH= "hotfixpath";

    /**
     * 展示人脸提示框
     */
    public static final String SHOW_LIVENESS_DIALOG_TIME = "show_livness_dialog_time";
    /**
     * 首页已经看过的重大公告标题
     */
    public static String READED_NOTICE = "updateTime";
    /**
     * 绑卡换卡标志位
     */

    public static final String ISCHANGE = "isChange";

    /**
     * 是否显示手势轨迹
     */
    public static final String SHOWGESTURETRAIL="trail";

//    private static SharedPreferences mSharedPreferences;
//
//    private static synchronized SharedPreferences getPreferneces() {
//        if (mSharedPreferences == null) {
//            mSharedPreferences = MainApplication.getInstance().getSharedPreferences("config", Context.MODE_PRIVATE);
//        }
//        return mSharedPreferences;
//    }

    /**
     * 打印所有
     */
//    public static void print() {
////		System.out.println(getPreferneces().getAll());
//    }

    /**
     * 清空保存在默认SharePreference下的所有数据
     */
//    public static void clear() {
//        getPreferneces().edit().clear().commit();
//    }

    /**
     * 保存字符串
     *
     * @return
     */
    public static void putString(String key, String value,Context mContext) {
//        getPreferneces().edit().putString(key, value).commit();
        if(TextUtils.isEmpty(key) ){
            JJLog.e("PreferenceUtil [putString] key = null");
        }else if(TextUtils.isEmpty(value)){
            SecurityStorageUtil.removeString(key,mContext);
        }else{
            SecurityStorageUtil.putString(key,value,mContext);
        }

    }

    /**
     * 读取字符串
     *
     * @param key
     * @return
     */
    public static String getString(String key,Context mContext) {
        String s = SecurityStorageUtil.getString(key,mContext);
        if(TextUtils.isEmpty(s)){
            return "";
        }else {
            return s;
        }
    }



    /**
     * 保存布尔值
     *
     * @return
     */
    public static void putBoolean(String key, Boolean value,Context mContext) {
//        getPreferneces().edit().putBoolean(key, value).commit();
        SecurityStorageUtil.putString(key,value.toString(),mContext);
    }

    public static void putLong(String key, long value,Context mContext) {
//        getPreferneces().edit().putLong(key, value).commit();
        SecurityStorageUtil.putString(key,Long.toString(value),mContext);
    }

    public static long getLong(String key, long defValue,Context mContext) {
//        return getPreferneces().getLong(key, defValue);
        try {
            String s = SecurityStorageUtil.getString(key,mContext);
            if(s!=null){
                return Long.parseLong(s);
            }else{
                return 0;
            }

        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    public static String getCertificateVerison(Context mContext) {
        String version = SecurityStorageUtil.getString(CERTIFICATEVERISON,mContext);
        if (TextUtils.isEmpty(version)) {
            return "0.0";
        } else {
            return version;
        }
    }

    public static void setCertificateVerison(String version,Context mContext) {
        putString(CERTIFICATEVERISON, version,mContext);
    }

    public static String getCertificate(Context mContext) {

        return SecurityStorageUtil.getString(CERTIFICATE,mContext);
    }

    public static void setCertificate(String certificate,Context mContext) {
        putString(CERTIFICATE, certificate,mContext);
    }

    /**
     * t 读取布尔值
     *
     * @param key
     * @return
     */
    public static boolean getBoolean(String key, boolean defValue,Context mContext) {
//        return getPreferneces().getBoolean(key, defValue);
        try {
            String s = SecurityStorageUtil.getString(key,mContext);
            if(!TextUtils.isEmpty(s)){
                return Boolean.parseBoolean(s);
            }else{
                return defValue;
            }

        }catch (Exception e){
            e.printStackTrace();
            return defValue;
        }

    }

    /**
     * 移除字段
     *
     * @return
     */
    public static void removeString(String key,Context mContext) {
//        getPreferneces().edit().remove(key).commit();
        SecurityStorageUtil.removeString(key,mContext);
    }

    /**
     * 存放实体类以及任意类型
     *
     * @param context 上下文对象
     * @param key
     * @param obj
     */
    public static void putBean(Context context, String key, Object obj,Context mContext) {
        if (obj instanceof Serializable) {// obj必须实现Serializable接口，否则会出问题
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(obj);
                String string64 = new String(Base64.encode(baos.toByteArray(),
                        0));
//                SharedPreferences.Editor editor = getPreferneces().edit();
//                editor.putString(key, string64).commit();
                SecurityStorageUtil.putString(key,string64,mContext);
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            throw new IllegalArgumentException(
                    "the obj must implement Serializble");
        }

    }

    /**
     * 获取实体
     *
     * @param context
     * @param key
     * @return
     */
    public static Object getBean(Context context, String key,Context mContext) {
        Object obj = null;
        try {
            String base64 = SecurityStorageUtil.getString(key,mContext);
            if (base64.equals("")) {
                return null;
            }
            byte[] base64Bytes = Base64.decode(base64.getBytes(), 1);
            ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            obj = ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
}
